var printing = false;
var printtext = "";
var printprocess = 0;
$(document).ready(function(){  
	$("#functionNav").attr("href",navAddr);

	getUserRight();
	var roleid = userInfo.roleid;
	var doms_cpfl = ["cpfl1","cpfl2","cpfl3"];
	selectThree(url_cplb,doms_cpfl);
	//获取并填充保管部门
	var bgbmJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do",{'tableName':'jb_bgbm'});
	loadSelect("#bgbm",bgbmJson.rows,'id','name');
	
	//根据保管部门id获取并填充库房
	$("#bgbm").change(function(){
		var bgbmid = $(this).val();
		if(bgbmid !=null && bgbmid !=''){
			var kfJson = ajaxResult("get",false,ProjectName+"/sysmanage.getkfIdNameBybgbmId.do",{'bgbmId':bgbmid});
			loadSelect("#szkf",kfJson.rows,'kfid','kfmc');
		}
	});
	  
	var params =$("#form1").serializeObject();
    //初始化出退库单据Table
	params = $.extend(true, {"cols":"cpfl1,cpjb,szkf"}, params);  
	createTable('#listTable',ProjectName+'/zzquery.getZongZhangList.do','id',cpTitle,params);	
	//点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#listTable','#form1');
	 });
	 $("#print_btn").click(function(){
		 //显示进度条
		 if(!printing){
			 if(confirm("开始准备打印,打印过程中请不要关闭页面,否则打印将被中断，确认进行打印？")){
				 $("#procssbarModal").modal('show');				 
				 printtext+="准备打印,打印过程中请不要关闭页面,否则打印将会中断\n";
				 //给出提示，时间较长，不能关闭页面。
				 print();
				 printing = false;
//				 $("#procssbarModal").modal('hide');
			 }
		 }
	 });
	 run();
})

function print(){
	//创建本地文件夹
	 var fso = "";
	 // 创建FileSystemObject对象实例
	 fso = new ActiveXObject("Scripting.FileSystemObject");
	 var downloadpath = "D:\\download";
	 try{			 
		 if (!fso.FolderExists(downloadpath))
			 fso.CreateFolder (downloadpath);
	 }catch(e){}
	 //获取所有的总登记号
	 var params =$("#form1").serializeObject();
	 var zdjhobj = ajaxResult("get",false,ProjectName+"/zzquery.getZongZhangZdjhList.do",params);
	 var perp = 100/zdjhobj.rows.length;
	 
	 for(var i=0;i<zdjhobj.rows.length;i++){
		 printprocess+=perp;
		 var zdjh = zdjhobj.rows[i].zdjh;
		 printtext+="准备下载"+zdjh+"\n";
		 //循环生成Excel
		 var url = ProjectName+"/zzquery.exportExcelCp_print.do?zdjh="+zdjh+"&username="+userInfo.username;
		 var r = ajaxResult("get",false,url,{});
		 //下载服务器文件到本地文件夹
		 if(r.status=="ok"){
			 var filename = r.data.filename;
			 var filepath = downloadpath+"\\"+filename;
			 var bf2=new BinaryFile(filepath);
			 bf2.WriteAll(r.data.content);
			 //开始打印
			 try{				 
				 xlPrint(filepath);
			 }catch(e){printtext+="打印时出现错误"+zdjh+"\n";break;}
			 printtext+="打印完成"+zdjh+"\n";
		 }else{
			 printtext+="导出文件错误:"+zdjh+":"+r.error+"\n";
		 }
		 printtext+="全部打印完成\n";
	 }
}
function run(){  
	$("#text_procssbar").val(printtext);
	var bar = document.getElementById("bar11"); 
	var total = document.getElementById("total"); 
	bar.style.width=parseInt(bar.style.width) + printprocess + "%";  
	total.innerHTML = bar.style.width; 
	if(bar.style.width >= "100%"){  
		window.clearTimeout(timeout); 
		return; 
	} 
	var timeout=window.setTimeout("run()",100); 
} 

function BinaryFile(filepath){
  var adTypeBinary=1,adTypeText=2;
  var adSaveCreateNotExist=1,adSaveCreateOverWrite=2;
  var adReadAll=-1,adReadLine=-2;
  this.path=filepath;
  this.WriteAll = function(content){
    var Stream = new ActiveXObject("ADODB.Stream");
    Stream.Type = adTypeText;
    Stream.CharSet = "iso-8859-1";
    Stream.Open();
    Stream.WriteText(content);
    Stream.SaveToFile(this.path, adSaveCreateOverWrite);
    Stream.Close();
    Stream = null;
  }
  this.ReadAll = function(){
    var Stream = new ActiveXObject("ADODB.Stream");
    Stream.Type = adTypeText;
    Stream.CharSet = "iso-8859-1";
    Stream.Open();
    try{alert(0);
    	Stream.LoadFromFile(this.path);
    }catch(e){alert("下载文件错误："+e);}alert(1);
    var content = Stream.ReadText(adReadAll);alert(2);
    Stream.Close();
    Stream = null;
    return content;
  }
}

function xlPrint(filepath){
	var xlApp;//存放Excel对象
	var XlBook;//存放Excel工作薄文件
	var xlSheet;//存放Excel活动工作表
	try{
		xlApp = new ActiveXObject("Excel.Application");
	}catch(e){
		alert("请启用ActiveX控件");
		return;
	}
//	alert("初始化控件完成");
	xlBook=xlApp.Workbooks.open(filepath);
//	alert("已经打开文件");
	var objsheet =  xlBook.Worksheets(1);
//	alert("获取第一个标签页");
	xlApp.DisplayAlerts = false;
	xlApp.Visible =true;
	try{
		objsheet.Visible = true;
		objsheet.PrintOut;//sheet打印
		xlBook.Close(true);
//		objsheet.Application.Quit();
		xlApp.Quit();
		xlApp=null;
	}catch(e){
		alert("打印时出错"+e);
	}
}

var cpTitle = [{
    checkbox: true
}, {
	field: 'zdjh',
	title: '藏品总登记号'
},{
	field: 'flh',
	title: '分类号'
},{
	field: 'cpmc',
	title: '藏品名称'
},{
	field: 'cpfl1',
	title: '藏品分类'
},{
	field: 'cpjb',
	title: '藏品级别'
},{
	field: 'szkf',
	title: '所在库房'
}]